﻿package plugin.instances.game
{
    import com.core.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.utils.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.instances.*;
    import uiresource.instances.ui.*;

    public class YwzlGame extends YWzlUI implements IInstance
    {
        private var bossArr:Array;

        public function YwzlGame()
        {
            txt_desc.addEventListener(TextEvent.LINK, this.doLink);
            return;
        }// end function

        protected function doLink(event:TextEvent) : void
        {
            var _loc_2:* = event.text;
            var _loc_3:* = _loc_2.split(",");
            ModelProxy.change("plugin", "open", {target:_loc_3[0], data:_loc_3[1]});
            return;
        }// end function

        public function initShow(param1:Object, param2:Dictionary) : void
        {
            if (param2[InstancesPublic.YWZLINIT])
            {
                this.updateShow(InstancesPublic.YWZLINIT, param2[InstancesPublic.YWZLINIT]);
            }
            else
            {
                txt_score.text = "0";
                this.bossArr = [];
                this.showBoss();
            }
            return;
        }// end function

        public function updateShow(param1:int, param2:Object) : void
        {
            switch(param1)
            {
                case InstancesPublic.TIMECHECKER:
                {
                    txt_time.text = TimeDataFormatUtil.getLeftTime(int(param2));
                    break;
                }
                case InstancesPublic.YWZLINIT:
                {
                    txt_score.text = param2.score;
                    this.bossArr = param2.boss;
                    this.showBoss();
                    break;
                }
                case InstancesPublic.YWZLBOSSKILL:
                {
                    this.bossArr[(param2.id - 1)] = param2.status;
                    this.showBoss();
                    break;
                }
                case InstancesPublic.YWZLSCORECHANGE:
                {
                    txt_score.text = param2 + "";
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function showBoss() : void
        {
            var _loc_1:* = ConfigDictionary.data.yiwangzoulang;
            this.doSort(_loc_1);
            bossMsg.list_msg.array = _loc_1;
            bossMsg.list_msg.renderHandler = new Handler(this.bossListRender);
            return;
        }// end function

        private function doSort(param1:Array) : void
        {
            var _loc_2:Object = null;
            if (this.bossArr)
            {
                for each (_loc_2 in param1)
                {
                    
                    _loc_2.curState = this.getState(_loc_2);
                }
            }
            param1.sortOn(["curState", "type", "jifen"], Array.DESCENDING | Array.DESCENDING | Array.DESCENDING);
            return;
        }// end function

        public function getState(param1:Object) : int
        {
            var _loc_2:int = 0;
            if (param1.type == 1)
            {
                _loc_2 = Number(ModelProxy.getValue("data", "servertime") + getTimer()) / 1000;
                if (_loc_2 >= int(this.bossArr[(param1.id - 1)]))
                {
                    return 9;
                }
                return 1;
            }
            else if (param1.type == 2)
            {
                if (int(this.bossArr[(param1.id - 1)]) > 0)
                {
                    return 1;
                }
                if (int(this.bossArr[(param1.id - 1)]) == 0)
                {
                    return 9;
                }
                return 5;
            }
            return 1;
        }// end function

        private function bossListRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Label = null;
            var _loc_5:Label = null;
            var _loc_6:Label = null;
            var _loc_7:Object = null;
            if (param2 < bossMsg.list_msg.length)
            {
                _loc_3 = bossMsg.list_msg.array[param2];
                _loc_4 = param1.getChildByName("bossname") as Label;
                _loc_5 = param1.getChildByName("score") as Label;
                _loc_6 = param1.getChildByName("state") as Label;
                _loc_7 = ConfigDictionary.data.monster;
                _loc_4.text = "<font color=\'#52B730\'><u><a href=\'event:1&" + UserData.getUserData("mapid") + "#" + _loc_3.mx + "#" + _loc_3.my + "\'>" + _loc_7[_loc_3.mid].name + "</a></u></font>";
                _loc_4.removeEventListener(TextEvent.LINK, this.linkHandler);
                _loc_4.addEventListener(TextEvent.LINK, this.linkHandler);
                _loc_5.text = _loc_3.jifen;
                switch(_loc_3.curState)
                {
                    case 1:
                    {
                        break;
                    }
                    case 5:
                    {
                        break;
                    }
                    case 9:
                    {
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        protected function linkHandler(event:TextEvent) : void
        {
            var _loc_2:Array = null;
            var _loc_3:Array = null;
            _loc_2 = event.text.split("&");
            switch(int(_loc_2[0]))
            {
                case 1:
                {
                    _loc_3 = _loc_2[1].split("#");
                    ModelProxy.change("zone", "stopGuaji", null);
                    ModelProxy.change("data", "taskfindmaster", _loc_3);
                    ModelProxy.change("zone", "autoMove", {x:_loc_3[1], y:_loc_3[2], tostageid:_loc_3[0], onhorse:true});
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function destroyShow(param1:Dictionary) : void
        {
            return;
        }// end function

        public function initEvent() : void
        {
            btn_tuichu.clickHandler = new Handler(this.doTuichu);
            bossMsg.btn_show.addEventListener(MouseEvent.CLICK, this.changeShow);
            return;
        }// end function

        protected function changeShow(event:Event) : void
        {
            if (bossMsg.box_msg.visible)
            {
                bossMsg.box_msg.visible = false;
                bossMsg.btn_show.url = "png.common.upArrowOverSkin";
            }
            else
            {
                bossMsg.box_msg.visible = true;
                bossMsg.btn_show.url = "png.common.downArrowUpSkin";
            }
            return;
        }// end function

        private function doTuichu() : void
        {
            ModelProxy.change("zone", "exitInstance", null);
            return;
        }// end function

        public function removeEvent() : void
        {
            return;
        }// end function

    }
}
